/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.telsoft.data;

import com.telsoft.business.DAOHelperBusiness;
import com.telsoft.entity.AttributesEntity;
import com.telsoft.entity.ChildrenEntity;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author thieplv
 */
public class AttributesDAO {
 private Connection conn;
 public List<AttributesEntity> getAttrs() throws SQLException{
     List<AttributesEntity> attrs = new ArrayList<AttributesEntity>();
     conn = DAOHelperBusiness.getConnection();
     Statement stm = DAOHelperBusiness.getStatement(conn);
     String sqlAttr = "SELECT * FROM ATTRIBUTES";
     String sqlChildAttr = "SELECT * FROM CHILD_ATT";
     ResultSet rsAttr = stm.executeQuery(sqlAttr);
     ResultSet rsChildAttr = stm.executeQuery(sqlChildAttr);
     while(rsAttr.next()){
         AttributesEntity attr = new AttributesEntity();
         List<Integer> childIDs = new ArrayList<Integer>();
         while(rsChildAttr.next()){
             if(rsAttr.getInt("ATT_ID") == rsChildAttr.getInt("ATT_ID")){
              childIDs.add(rsChildAttr.getInt("CHILD_ID"));   
             }
         }
         attr.setChildIDs(childIDs);
         attr.setAttID(rsAttr.getInt("ATT_ID"));
         attr.setDescription(rsAttr.getString("DESCRIPTION"));
         attr.setName(rsAttr.getString("NAME"));
         attrs.add(attr);
     }
     DAOHelperBusiness.closeConnection(conn);
     DAOHelperBusiness.closeStatement(stm);
     DAOHelperBusiness.closeResultSet(rsAttr);
     DAOHelperBusiness.closeResultSet(rsChildAttr);   
     return attrs;
 }
}
